package design_pattern.ReadWriteLock;

import java.util.Random;

/**
 * @Author: 蔡翔
 * @Date: 2019/11/4 10:57
 * @Version 1.0
 */
public class WriteWork extends Thread{
    private static final Random random = new Random(System.currentTimeMillis());
    private final ShareDate date;
    private final String filter;
    private int index = 0;
    public WriteWork(ShareDate shareDate,String filter){
        this.date = shareDate;
        this.filter = filter;
    }

    @Override
    public void run() {
        try {
            while (true){
                char c = nextChar();
                date.write(c);
                Thread.sleep(1000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private char nextChar(){
        char c = filter.charAt(index);
        index++;
        if(index>=filter.length()){
            index = 0;
        }
        return c;
    }
}
